package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Jt
 * @date 2021/4/9 19:46
 */
@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据条件查询频道列表
     *
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
//        1.参数的校验 (非空校验,分页信息的校验)
        if (channelDto == null) {
//            说明参数无效
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDto.checkParam();// 检查分页
//        2.查询条件的封装
//        2.1 分页条件
        Page<AdChannel> pageResult = new Page<>(channelDto.getPage(), channelDto.getSize());
//        2.2 name模糊查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(channelDto.getName())) {
            queryWrapper.like(AdChannel::getName, channelDto.getName());
        }
//        3.执行分页查询
        IPage<AdChannel> page = page(pageResult, queryWrapper);
//        4.封装结果返回
        PageResponseResult result = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }


    /**
     * 新增
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
//        1.参数校验
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        adChannel.setCreatedTime(new Date());
        if (adChannel.getIsDefault() == null) {
            adChannel.setIsDefault(false);
        }
        if (adChannel.getIsDefault()) {
            AdChannel one = getOne(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getIsDefault, 1));
            if (one != null) {
                one.setIsDefault(false);
                updateById(one);
            }
        }
//        2.执行新增
        save(adChannel);
//        3.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
//        1.检查参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        2.先查后改
        AdChannel byId = getById(adChannel.getId());
        if (byId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        3.返回结果
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
//        1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        2.判断当前频道是否存在,和频道的状态是否有效
        AdChannel byId = getById(id);
        if (byId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
//        2.1 如果有效则不能删除
        if (byId.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_USE);
        }
//        System.out.println(1/0);
//        3.删除频道
        removeById(id);
//        测试
        /*if (id == 5) {
            throw new CustomException(AppHttpCodeEnum.DATA_USE);
        }*/
//        4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
